package org.duiduo.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.duiduo.annotation.LogPush;
import org.duiduo.pojo.TbUser;
import org.duiduo.repository.TbUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 添加修改的切面
 * 
 * @author 夜泷noga
 */
@Slf4j
@Aspect
@Component
public class LogPushAspect {
	private static final String ADD = "ADD";
    private static final String EDIT = "EDIT";
    private static final String EDITADD = "EDITADD";
    
    /**
     * id
     */
    private String id;
    
    /**
     * 用户名
     */
    private String username;
    
    /**
     * 密码
     */
    private String password;
    
    /**
     * 旧用户名
     */
    private String oldUsername;
    
    /**
     * 旧密码
     */
    private String oldPassword;
    
    /**
     * 类型
     */
    private String type;
    
    @Autowired
    private TbUserRepository repository;
    
    @Pointcut(value = "@annotation(org.duiduo.annotation.LogPush)")
    public void pointCut() {
    	
    }
    
    @Before("pointCut()")
    public void beforeMethod(JoinPoint joinPoint) {
    	Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        LogPush logPush = methodSignature.getMethod().getAnnotation(LogPush.class);
        type = logPush.type().toUpperCase();
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
			if (arg instanceof TbUser) {
				TbUser user = (TbUser) arg;
				if (EDIT.equals(type) || EDITADD.equals(type)) {
					if (user.getId() != null) {
						id = String.valueOf(user.getId());
						TbUser oldUser = repository.findById(Integer.parseInt(id)).get();
						oldUsername = oldUser.getUsername();
						oldPassword = oldUser.getPassword();
					}
					username = user.getUsername();
                    password = user.getPassword();
                }
                if (ADD.equals(type)) {
                	username = user.getUsername();
                    password = user.getPassword();
                }
                break;
			}
		}
    }
    
    @Async
    @After("pointCut()")
    public void afterMethod() {
    	if (EDIT.equalsIgnoreCase(type) || EDITADD.equalsIgnoreCase(EDITADD)) {
    		if (id == null) {
    			addLog();
    		} else {
	    		if (!username.equals(oldUsername)) {
	    			log.info("用户名username由原来的:{}更改为:{}", oldUsername, username);
	    		}
	    		if (!password.equals(oldPassword)) {
	    			log.info("密码password由原来的:{}更改为:{}", oldPassword, password);
	    		}
    		}
    	}
    	if (ADD.equalsIgnoreCase(type)) {
    		addLog();
    	}
    }
    
    public void addLog() {
    	log.info("添加用户名username:{}", username);
		log.info("添加密码password:{}", password);
    }
}
